package com.hlk.article.controller;

import com.hlk.article.service.CommentPortalService;
import com.hlk.article.utils.RestTemplateUtils;
import com.hlk.constant.Constant;
import com.hlk.controller.article.CommentControllerApi;
import com.hlk.feign.UserFeignClient;
import com.hlk.pojo.bo.CommentReplyBO;
import com.hlk.pojo.vo.AppUserVO;
import com.hlk.result.Result;
import com.hlk.utils.JsonUtils;
import com.hlk.utils.PageResult;
import com.hlk.utils.RedisCountUtils;
import com.hlk.utils.ValidateUtils;
import com.hlk.utils.redis.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author hlk
 * @version 1.0
 * @date 2021-01-08 15:00
 */
@RestController
public class CommentController implements CommentControllerApi {

    @Autowired
    private CommentPortalService commentPortalService;

    @Autowired
    private RedisCountUtils redisCountUtils;

    @Autowired
    private UserFeignClient userFeignClient;


    @Override
    public Result createArticle(@Valid CommentReplyBO commentReplyBO, BindingResult result) {
        // 校验参数是否符合
        if (result.hasErrors()) {
            Map<String, String> map = ValidateUtils.checkParams(result);
            return Result.errorMap(map);
        }

        // 1. 根据留言用户的id查询他的昵称，用于存入到数据表进行字段的冗余处理，从而避免多表关联查询的性能影响
        String userId = commentReplyBO.getCommentUserId();

        // 2. 发起restTemplate调用用户服务，获得用户侧昵称
        Set<String> idSet = new HashSet<>();
        idSet.add(userId);
//        AppUserVO appUserVO = restTemplateUtils.getPublisherList(idSet).get(0);
        Result bodyResult = userFeignClient.queryByIds(JsonUtils.objectToJson(idSet));
        List<AppUserVO> userVOList = null;
        if (bodyResult.getStatus() == 200) {
            String userJson = JsonUtils.objectToJson(bodyResult.getData());
            userVOList = JsonUtils.jsonToList(userJson, AppUserVO.class);
        }

        String nickname = userVOList.get(0).getNickname();
        String face = userVOList.get(0).getFace();

        // 3. 保存用户评论的信息到数据库
        commentPortalService.createComment(commentReplyBO.getArticleId(),
                commentReplyBO.getFatherId(),
                commentReplyBO.getContent(),
                userId,
                nickname,
                face);

        return Result.ok();
    }

    @Override
    public Result commentCounts(String articleId) {
        Integer counts = redisCountUtils.getCountFromRedis(RedisKeys.getArticleCommentCounts(articleId));
        return Result.ok(counts);
    }

    @Override
    public Result list(String articleId, Integer page, Integer pageSize) {
        if (page == null) {
            page = Constant.PAGE;
        }
        if (pageSize == null) {
            pageSize = Constant.PAGESIZE;
        }

        PageResult pageResult = commentPortalService.queryArticleComments(articleId, page, pageSize);
        return Result.ok(pageResult);
    }

    @Override
    public Result managerCommentList(String writerId, Integer page, Integer pageSize) {
        if (page == null) {
            page = Constant.PAGE;
        }
        if (pageSize == null) {
            pageSize = Constant.PAGESIZE;
        }

        PageResult pageResult = commentPortalService.queryWriterCommentsMng(writerId, page, pageSize);
        return Result.ok(pageResult);
    }

    @Override
    public Result delete(String writerId, String commentId, String articleId) {
        commentPortalService.deleteComment(writerId, commentId, articleId);
        return Result.ok();
    }
}
